<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/core" prefix="c"%>
<%@ taglib uri="/tags/fmt" prefix="fmt"%>
<script type="text/javascript" src="js/utils.js"></script>
<script type="text/javascript">         
    function validate(){
        var errors = "";
        var plantId = document.getElementById('plantId').value;
        var shortName = document.getElementById('shortName').value;
        var description = document.getElementById('description').value;
        var storageAmount = document.getElementById('storageAmount').value;
        var unloadAmount = document.getElementById('unloadAmount').value;
        var grindProtectionDays = document.getElementById('grindProtectionDays').value;
        var priority = document.getElementById('priority').value;                                        

        if(plantId == ""){                
        	errors = ' <bean:message key="lable.plants.validation.number"/>\n';			  
        }      
        if(shortName == ""){                
        	errors += ' <bean:message key="lable.plants.validation.code"/>\n';			  
        }
		if(description == "") {
        	errors += ' <bean:message key="lable.plants.validation.description"/>\n';
        } 
        if(storageAmount == "" || parseInt( storageAmount ) == 0) {
        	errors += ' <bean:message key="lable.plants.validation.storage.amount"/>\n';
        }
        if(unloadAmount == "" || parseInt( unloadAmount ) == 0) {
        	errors += ' <bean:message key="lable.plants.validation.max.unload"/>\n';
        }
		if(grindProtectionDays == "" || parseInt( grindProtectionDays ) == 0) {
        	errors += ' <bean:message key="lable.plants.validation.grind.protection.days"/>\n';
        } else if (parseInt( grindProtectionDays ) > 100) {
        	errors += ' <bean:message key="lable.plants.validation.grind.protection.days"/>\n';
        }
        if(priority == "" || parseInt( priority ) == 0) {
        	errors += ' <bean:message key="lable.plants.validation.priority"/>\n';
        } else if (parseInt( priority ) > 10) {
        	errors += ' <bean:message key="lable.plants.validation.priority"/>\n';
        }
        if( errors == "" ){
            return true;
        }else{
            alert( '<bean:message key="label.common.errors"/> : \n' + errors );
			return false;
        }
	}
    function guardarNuevo(forma){
    	forma.saveandnew.value = 1;
    }
</script>

<html:form action="/addPlant.do" onsubmit="return validate();">
	<input name="saveandnew" type="hidden" value="0"/>
	<table align="center" width="40%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr>
			<th colspan="2" align="center" class="titulocolumna"> <bean:message key="label.plants.add.table.title" /> </th>			 
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.plants.add.column.number"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text styleId="plantId" property="plantView.plantId" maxlength="4" size="3" style="text-transform: uppercase;" styleClass="columnanumero" onfocus="this.select();" onkeydown="return (numeric(event, 0) && noPaste(event, this));" onkeyup="extractNumber(this,0,false);"/></td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.plants.add.column.code"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text styleId="shortName" property="plantView.shortName" maxlength="3" size="3" style="text-transform: uppercase;" styleClass="campo_texto" onfocus="this.select();"/> </td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.plants.add.column.description"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text styleId="description" property="plantView.description" styleClass="campo_texto" maxlength="255" onfocus="this.select();"/> </td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.plants.add.column.storage.amount"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text styleId="storageAmount" property="plantView.storageAmount"  styleClass="columnanumero" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,3,false);"/> </td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.plants.add.column.max.unload"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text styleId="unloadAmount" property="plantView.unloadAmount" styleClass="columnanumero" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,3,false);"/> </td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.plants.add.column.grind.protection.days"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text styleId="grindProtectionDays" property="plantView.grindProtectionDays" maxlength="3" size="1" styleClass="columnanumero" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,0,false);" />  <bean:message key="label.plants.days"/> </td>			 
		</tr>
		<tr>
			<td class="etiqueta"> <bean:message key="label.plants.add.column.grind.priority"/> </td>
			<td class="etiqueta_campo" style="text-align: left;"> <html:text styleId="priority" property="plantView.priority" maxlength="2" size="1" styleClass="columnanumero" onfocus="this.select();" onkeydown="return (numeric(event, 1) && noPaste(event, this));" onkeyup="extractNumber(this,0,false);" /> </td>			 
		</tr>
		<tr>
			<td colspan="2" align="center" class="columnadatoPar">
				<html:submit styleClass="boton"><bean:message key="label.common.save"/></html:submit>
				<html:submit styleClass="boton_large" onclick="return guardarNuevo(this.form);"><bean:message key="label.common.saveAndNew"/></html:submit>
				&nbsp;&nbsp;&nbsp;&nbsp;<html:button styleClass="boton" property="backButton" onclick="location.href='listPlantsQuery.do';"><bean:message key="label.common.cancel"/></html:button>
			</td>
		</tr>		
	</table>
</html:form>
<script>
	cleanNumber(document.getElementById('plantId'));
	cleanNumber(document.getElementById('storageAmount'));
	cleanNumber(document.getElementById('unloadAmount'));
	cleanNumber(document.getElementById('grindProtectionDays'));
	cleanNumber(document.getElementById('priority'));
</script>